package com.example.storage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.collector.RequestInfo;

public class RediesMetricsStorate implements MetricsStorage {

    private Map<String, ArrayList<RequestInfo>> requestInfos = new HashMap<String, ArrayList<RequestInfo>>();

    public RediesMetricsStorate() {
        
    }

    @Override
    public void saveData(RequestInfo info) {
        //...
        if (!requestInfos.containsKey(info.getApiName())) {
            requestInfos.put(info.getApiName(), new ArrayList<RequestInfo>());
        }
        List<RequestInfo> infos = requestInfos.get(info.getApiName());
        infos.add(info);
    }

    @Override
    public Map<String, ArrayList<RequestInfo>> getDatas(double startTime, double endTime) {
        Map<String, ArrayList<RequestInfo>> resultDatas = new HashMap<String, ArrayList<RequestInfo>>();
        for (Map.Entry<String, ArrayList<RequestInfo>> entry : requestInfos.entrySet()) {
            ArrayList<RequestInfo> infos = entry.getValue();
            ArrayList<RequestInfo> resultInfos = new ArrayList<RequestInfo>();
            for (RequestInfo info : infos) {
                if (info.getStartTime() >= startTime && info.getStartTime() <= endTime) {
                    resultInfos.add(info);
                }
            }
            resultDatas.put(entry.getKey(), resultInfos);
        }
        return resultDatas;
    }

}
